<?php   if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class article_model extends CI_Model{

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    function getArticleById($articleId){
        $this->db->select('sy_content.*');
        $this->db->from('sy_content');
        $this->db->where('sy_content.id',$articleId);
        $query = $this->db->get();
        if($query->num_rows() > 0) return $query->row_array();
        return false;
    }
    
    function getAllListArticle($itemsPerPage,$start){
        $this->db->select('sy_content.*');
        $this->db->select('sy_content_category.name As category_name');
        $this->db->select('sy_users.username As username');
        $this->db->from('sy_content');
        $this->db->join('sy_content_category', 'sy_content_category.id = sy_content.category_id');
        $this->db->join('sy_users', 'sy_users.id = sy_content.auth_id');
        $this->db->where('sy_content.status !=',-1);
        $this->db->order_by("sy_content.id", "desc");
        $this->db->limit($itemsPerPage, $start);

        $query = $this->db->get();
        
        if($query->num_rows() > 0) return $query->result();
        return false;
    }
    
    function countAllListArticle(){
        $this->db->select('sy_content.*');
        $this->db->from('sy_content');
        $this->db->join('sy_content_category', 'sy_content_category.id = sy_content.category_id');
        $this->db->join('sy_users', 'sy_users.id = sy_content.auth_id');
        $this->db->where('sy_content.status !=',-1);
        $this->db->order_by("sy_content.id", "desc");

        //$query = $this->db->get();
        return $this->db->count_all_results();
    }
    
    function insertArticle($data){
        $this->db->insert('sy_content', $data);
        return $this->db->insert_id();
    }
    
    function updateArticle($id,$data){
        $this->db->where('id', $id);
        $this->db->update('sy_content', $data); 
    }
    
    function getAllCategoryBySection(){
        $this->db->select('sy_content_category.*');
        $this->db->select('sy_content_section.name As section_name');
        $this->db->from('sy_content_category');
        $this->db->join('sy_content_section', 'sy_content_section.id = sy_content_category.section_id');
        $this->db->where('sy_content_category.status !=',-1);
        $this->db->where('sy_content_section.status !=',-1);
        //$this->db->order_by("sy_content.id", "desc");
        $query = $this->db->get();
        
        if($query->num_rows() > 0){
            $items = $query->result_array();
            $data = array();
            foreach($items As $item){
                $data[$item['section_id']][] = array('value'=>$item,'section'=>$item['section_name']);
            }
            return $data;
        }else{
            return false;
        }
    }
}